
package com.netease.xone.itemview;

import protocol.meta.UserDynamic;
import Utils.TimeFormatUtil;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.netease.xone.R;
import com.netease.xone.widget.LoadingImageView;

public class RenderDynamicView {

    private TextView dynamicInfoText;
    private TextView dynamicTime;
    private LoadingImageView userAvatarImage;
    private TextView dynamicTimelineImage;
    private ImageView dynamicAvatarBorder;
    private LinearLayout dynamicInfoBg;

    public RenderDynamicView(View v) {
        dynamicInfoText = (TextView)v.findViewById(R.id.dynamic_info);
        dynamicTime = (TextView)v.findViewById(R.id.dynamic_time);
        userAvatarImage = (LoadingImageView)v.findViewById(R.id.dynamic_user_avatar);
        dynamicAvatarBorder = (ImageView)v.findViewById(R.id.dynamic_user_avatar_border);
        dynamicTimelineImage = (TextView)v.findViewById(R.id.dynamic_timeline_image);
        dynamicInfoBg = (LinearLayout)v.findViewById(R.id.dynamic_info_bg);
        // 初始化
        int dimension = (int)v.getResources().getDimension(R.dimen.common_avatar_size);
        userAvatarImage.setServerClipSize(dimension, dimension);
    }

    public void renderView(final Context context, final UserDynamic dynamic,
            boolean sameWithPrevious, boolean isTheLastOne) {
        if (dynamic == null)
            return;
        if (sameWithPrevious) {
            // 屏蔽头像
            userAvatarImage.setVisibility(View.INVISIBLE);
            dynamicAvatarBorder.setVisibility(View.INVISIBLE);
        } else {
            userAvatarImage.setVisibility(View.VISIBLE);
            dynamicAvatarBorder.setVisibility(View.VISIBLE);
            userAvatarImage.setImageResource(R.drawable.avatar_user_default_50);
            if (dynamic.userVO != null && !TextUtils.isEmpty(dynamic.userVO.avatar)) {
                userAvatarImage.setLoadingImage(dynamic.userVO.avatar);
            }
        }
        String userName = dynamic.userVO == null ? "" : dynamic.userVO.nickname;
        String dynamicInfo = userName + dynamic.dynamicInfo;
        dynamicInfoText.setText(dynamicInfo, BufferType.SPANNABLE);
        Spannable span = (Spannable)dynamicInfoText.getText();
        int start = 0;
        int end = userName.length();
        span.setSpan(new ForegroundColorSpan(Color.WHITE), start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        dynamicTime.setText(TimeFormatUtil.getTimeFormat(context, dynamic.createTime));
        if (isTheLastOne) {
            dynamicTimelineImage.setBackgroundResource(R.drawable.bg_timeline_bottom);
        } else {
            dynamicTimelineImage.setBackgroundResource(R.drawable.bg_timeline_top);
        }
        // userName.setText(user.nickname);
        // userSign.setText(user.signature);
        // if (userFansButton != null) {
        // userFansButton.setOnClickListener(new OnClickListener() {
        //
        // @Override
        // public void onClick(View v) {
        // // 关注或者取消关注
        // if (user.follow == 0) {
        // // 我未关注他，发起关注
        // XoneService.getInstance().doFollow(user.userId);
        // } else if (user.follow == 1) {
        // // 我已经关注他，发起取消关注
        // XoneService.getInstance().doCancelFollow(user.userId);
        // }
        //
        // }
        // });
        // if (user.follow == 0) {
        // // 我未关注他
        // // 以下这两种状态暂时相同，没有对方已关注我而我没关注对方这个状态
        // userFansButton.setBackgroundResource(R.drawable.selector_blue_btn);
        // userFansButtonText.setCompoundDrawablePadding((int)(5 * density));
        // userFansButtonText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_add,
        // 0, 0, 0);
        // userFansButtonText.setText(R.string.add_follow);
        // // if (user.followed == 0) {
        // // // 他未关注我
        // // } else if (user.followed == 1) {
        // // // 他已经关注我
        // // }
        // } else if (user.follow == 1) {
        // // 我已经关注他
        // userFansButton.setBackgroundResource(R.drawable.selector_gray_btn);
        // if (user.followed == 0) {
        // // 他未关注我
        // userFansButtonText.setCompoundDrawablePadding(0);
        // userFansButtonText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0,
        // 0);
        // userFansButtonText.setText(R.string.has_followed);
        // } else if (user.followed == 1) {
        // // 他已经关注我
        // userFansButtonText.setCompoundDrawablePadding((int)(2 * density));
        // userFansButtonText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_exchange,
        // 0, 0, 0);
        // userFansButtonText.setText(R.string.followed_eachother);
        // }
        // }
        // }

    }
}
